import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { AlarmItemModel } from '#/api/equipment/models';
import type { DictModel } from '#/api/system/models';

import { isUndefined } from '@vben/utils';

import { useAlarmLevelOptions } from '#/api/equipment/models';
import { getLineListApi } from '#/api/master/line';
import { getDictByCodeApi } from '#/api/system/dict';

/** 获取表格查询表单的字段配置。 */
export function useSearchSchema(): VbenFormSchema[] {
  return [
    {
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        labelField: 'lineName',
        valueField: 'lineCode',
        api: getLineListApi,
        immediate: false,
      },
      fieldName: 'lineCode',
      label: '生产线',
    },
    {
      component: 'Input',
      componentProps: {
        allowClear: true,
      },
      fieldName: 'workCenter',
      label: '工作中心',
    },
  ];
}

/**
 * 获取编辑表单的字段配置。
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        labelField: 'lineName',
        valueField: 'lineCode',
        api: getLineListApi,
        immediate: false,
      },
      dependencies: {
        disabled(values) {
          return !isUndefined(values.id);
        },
        triggerFields: ['lineCode'],
      },
      fieldName: 'lineCode',
      label: '生产线',
      rules: 'required',
    },
    {
      component: 'Input',
      dependencies: {
        disabled(values) {
          return !isUndefined(values.id);
        },
        triggerFields: ['workCenter'],
      },
      fieldName: 'workCenter',
      label: '工作中心',
      rules: 'required',
      help: '区域、工位或设备',
    },
    {
      component: 'InputNumber',
      componentProps: {
        min: 1,
        precision: 0,
      },
      fieldName: 'index',
      label: '顺序号',
      rules: 'required',
      defaultValue: 1,
      help: '从 1 开始',
    },
    {
      component: 'Textarea',
      componentProps: {
        showCount: true,
        maxlength: 128,
      },
      fieldName: 'content',
      label: '警报内容',
      rules: 'required',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        showSearch: true,
        optionFilterProp: 'label',
        afterFetch: (data: DictModel) => {
          return data.items.map((item) => ({
            label: item.label,
            value: item.code,
          }));
        },
        api: getDictByCodeApi,
        params: { code: 'mes_alarm_category' },
        immediate: false,
      },
      fieldName: 'category',
      label: '报警分类',
      rules: 'required',
    },
    {
      component: 'RadioGroup',
      componentProps: {
        options: useAlarmLevelOptions(),
      },
      fieldName: 'alarmLevel',
      label: '警报级别',
      rules: 'required',
      defaultValue: 4,
    },
  ];
}

/**
 * 获取表格列配置。
 * @param onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onActionClick?: OnActionClickFn<AlarmItemModel>,
): VxeTableGridOptions<AlarmItemModel>['columns'] {
  return [
    { type: 'seq', width: 60 },
    { field: 'lineCode', title: '产线代码', width: 120 },
    { field: 'workCenter', title: '工作中心', width: 120 },
    { field: 'index', title: '顺序号', width: 80 },
    { field: 'content', title: '警报内容', minWidth: 180 },
    { field: 'category', title: '警报分类', width: 140 },
    {
      field: 'alarmLevel',
      title: '警报级别',
      width: 80,
      cellRender: {
        name: 'CellTag',
        options: useAlarmLevelOptions(),
      },
    },
    { field: 'createdTime', title: '创建时间', width: 150 },
    {
      cellRender: {
        attrs: {
          nameField: 'index',
          nameTitle: '警报定义',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          'edit', // 默认的编辑按钮
          'delete', // 默认的删除按钮
        ],
      },
      field: 'operation',
      fixed: 'right',
      title: '操作',
      width: 120,
    },
  ];
}
